<template>
  <a-modal
    title="编辑标签类型"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item v-show="false"><a-input v-decorator="['id']" /></a-form-item>
        <a-form-item
          label="标签名称"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input disabled placeholder="请输入名称" v-decorator="['name', {rules: [{required: true, message: '请输入名称！'}]}]" />
        </a-form-item>

        <a-form-item
          label="标签类型"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-select
            mode="multiple"
            :filterOption="filterOption"
            v-decorator="['tagTypeIds', {rules: [{required: true, message: '请选择标签类型！'}]}]">
            <a-select-option v-for="(item) in typeTreeData" :key="item.id" :value="item.id" >{{ item.value }}</a-select-option>
          </a-select>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { saveTagsType, queryTagsType } from '@/api/modular/main/poidatatags/poiDataTagsManage'
import { poiDataTagsTypeList } from '@/api/modular/main/poidatatagstype/poiDataTagsTypeManage'
import _ from 'lodash'
export default {
  data () {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 15 }
      },
      createDateDateString: '',
      modifyDateDateString: '',
      visible: false,
      confirmLoading: false,
      typeTreeData: [],
      form: this.$form.createForm(this)
    }
  },
  created() {
    this.loadTypeList()
  },

  methods: {
    // 初始化方法
    add (record) {
      this.visible = true
      setTimeout(() => {
        queryTagsType({ id: record.id }).then((res) => {
          if (res.success) {
            const array = _.map(res.data, 'typeId')
            this.form.setFieldsValue(
              {
                id: record.id,
                name: record.name,
                tagTypeIds: array
              }
            )
          }
        })
      }, 100)
    },
    loadTypeList() {
      poiDataTagsTypeList({}).then((res) => {
        this.typeTreeData = res.data
      })
    },
    filterOption(input, option) {
      if (option) {
        const boo = option.componentOptions.children[0].text.indexOf(input) >= 0
        return boo
      }
    },
    /**
     * 提交表单
     */
    handleSubmit () {
      const { form: { validateFields } } = this
      this.confirmLoading = true
      validateFields((errors, values) => {
        if (!errors) {
          saveTagsType(values).then((res) => {
            if (res.success) {
              this.$message.success('新增成功')
              this.confirmLoading = false
              this.$emit('ok', values)
              this.handleCancel()
            } else {
              this.$message.error('新增失败')// + res.message
            }
          }).finally((res) => {
            this.confirmLoading = false
          })
        } else {
          this.confirmLoading = false
        }
      })
    },
    onChangecreateDate(date, dateString) {
      this.createDateDateString = dateString
    },
    onChangemodifyDate(date, dateString) {
      this.modifyDateDateString = dateString
    },
    handleCancel () {
      this.form.resetFields()
      this.visible = false
    }
  }
}
</script>
